<template>
  <div class="weather-head">
    <div class="weather-head-left">
      <i class="iconfont icon-sun"  @click="goHome"></i>
      <div class="weather-head-title"  @click="goHome">新中地天气</div>
      <div class="weather-head-city" >{{ CityWeather.LocalCity.city }}</div>
      <div class="weather-head-weather">
        实时天气：{{ CityWeather.curWeather.weather }}
        {{ CityWeather.curWeather.temperature }}℃&nbsp;&nbsp;&nbsp;{{
          CityWeather.curWeather.winddirection
        }}风{{ CityWeather.curWeather.windpower }}级
      </div>
    </div>
    <div class="weather-head-right">
      <i class="iconfont icon-gantanhao" @click="dialogVisible = true"></i>
    </div>
    <Teleport to="body" v-if="dialogVisible">
      <div class="detail-bg" @click="handleClick">
        <div class="detail-box" ref="detailBox">
          <h5>关于:</h5>
          <p>这个应用可以用来追踪你选择城市的当前天气</p>
          <h5>如何使用:</h5>
          <p>1.点击搜索框输入你希望追踪的城市</p>
          <p>2.在搜索结果中选中一个城市，你将获取当地最新的天气</p>
          <p>3.点击右侧的＋号可以将追踪城市的天气情况保存在首页</p>
          <h5>移除城市:</h5>
          <p>如果你不想在首页关注某个城市,可以通过底部的按钮删除它</p>
          <button class="close" @click="dialogVisible = false">关闭</button>
        </div>
      </div>
    </Teleport>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useLocalWeatherStore } from "@/stores/LocalWeather";
import { useRouter } from "vue-router";

const CityWeather = useLocalWeatherStore();
const dialogVisible = ref(false);
const detailBox = ref("");

const router = useRouter(); 
const goHome = () => {
  router.push({ name: 'home' }); // 根据路由配置中的name跳转
};

const handleClick = (e) => {
  e.stopPropagation();
  if (e.target.classList.contains("detail-bg")) {
    dialogVisible.value = false;
  }
};
</script>

<style lang="scss" scoped>
@import "https://at.alicdn.com/t/c/font_4869878_glagdbt07ww.css";

.city-search{
    padding: 0 20%;
}
.weather-head {
  position: sticky;
  top: 0;
  background-color: #00668a;
  height: 80px;
  display: flex;
  padding: 0 20%;
  justify-content: space-between;
  align-items: center;
  color: white;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3);
  .weather-head-title {
    font-size: 1.5rem;
  }
  .weather-head-city {
    font-size: 1.125rem;
  }
  .weather-head-weather {
    font-size: 0.875rem;
  }
  .iconfont {
    font-size: 1.5rem;
    color: white;
  }
  .weather-head-left {
    display: flex;
    align-items: center;
    gap: 15px;
    .icon-sun, .weather-head-title {
    cursor: pointer;
    }
  }
  .weather-head-right {
    display: flex;
    gap: 15px;
    .iconfont {
      cursor: pointer;
    }
  }
}
.detail-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.6s ease;
  .detail-box {
    position: relative;
    width: 400px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    justify-content: center;
    h5 {
      font-size: 20px;
      margin-bottom: 10px;
    }

    p {
      font-size: 16px;
      margin-bottom: 20px;
    }

    button {
      width: 100px;
      height: 40px;
      border: none;
      background-color: #00668a;
      color: #fff;
      border-radius: 5px;
      font-size: 16px;
      cursor: pointer;
      transition: all 0.6s ease;
      &:hover {
        background-color: #00526e;
      }
    }
  }
}
</style>